package com.deer.wms.ware.task.model.pushErp.enter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.enums.ErpType;
import com.deer.wms.ware.task.model.pushErp.AbstractPushErpBean;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: xu
 * @Date: 2022/06/16/22:40
 * @Description: 调拨出库      41
 */
@Component
public class ALLOTEnterBeanAbstract extends AbstractPushErpBean {

    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private ItemInfoService itemInfoService;
    public ErpType gainType() {
        return new ErpType(null, 29);
    }


    protected  Boolean front(SoMaster soMaster, List<SoDetail> soDetail ){
        return true;
    }

    /**
     * 字段添加
     *  @param orderToErpBO
     * @param
     * @param
     */
    @Override
    protected void add(OrderToErpBO orderToErpBO, SoMaster soMaster, Map<String, SoDetail> soDetailMap) {
        //部门id
        orderToErpBO.setDeptId(Optional.ofNullable(soMaster.getDeptId()).orElse(null));
        //供应商id
        orderToErpBO.setSupplierId(Optional.ofNullable(soMaster.getSupplierId()).orElse(null));

        for (OrderDetailBO detail : orderToErpBO.getDetails()) {
            SoDetail soDetail = soDetailMap.get(detail.getDetailNo());
            ItemInfo itemInfo = itemInfoService.getOne(new QueryWrapper<ItemInfo>().eq("item_code", soDetail.getItemCode()));
            //物料id
            detail.setItemId(Optional.ofNullable(itemInfo).map(item -> item.getId()).orElse(null));

        }
    }
    protected void pushErp(OrderToErpBO orderToErpBO){
        pushErpService.CreateIMBill(orderToErpBO);
    }

}
